package project.ia04.agents.gui;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import project.ia04.utils.Data;
import project.ia04.utils.Staff;

public class XCommandAgent2StudioAgentBehaviour extends CyclicBehaviour {
	
	private boolean begin = true;

	@Override
	public void action() {
		if(begin){
			System.out.println("XAgent --> XCommandAgent2StudioAgentBehaviour (Send)");
			ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
			message.addReceiver(((XCommandAgent)myAgent).getEnvAgent());
			message.setConversationId(XCommandAgent.cstXCommand2ConvId[((XCommandAgent)myAgent).getCurrType()-7]);
			message.setContent(serialize());
			myAgent.send(message);
			begin = false;
		}
		else{
			MessageTemplate template = MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM),
									MessageTemplate.MatchConversationId(XCommandAgent.cstXCommand2ConvId[((XCommandAgent)myAgent).getCurrType()-7]));
			ACLMessage message = myAgent.receive(template);
			if(message != null){
				System.out.println("XAgent --> XCommandAgent2StudioAgentBehaviour (Reception)");
				((XCommandAgent)myAgent).send2CmdDialog(deserialize(message.getContent()));
			}
			else{
				template = MessageTemplate.and(MessageTemplate.MatchPerformative(ACLMessage.INFORM),
							MessageTemplate.MatchConversationId(Data.CMDTERMINATE));
				message = myAgent.receive(template);
				if(message != null){
					System.out.println("XAgent --> XCommandAgent2StudioAgentBehaviour (Reception-End)");
					((XCommandAgent)myAgent).CmdEnded();
				}
				else 
					block();
			}
				
		}

	}

	private HashMap<String, Integer> deserialize(String content) {
		ObjectMapper mapper = new ObjectMapper();
		Map<String,Object> map ;
		HashMap<String, Integer> res = new HashMap<String, Integer>();
		try {
			map = mapper.readValue(content,Map.class);
			res.put(Data.GAMEPLAYITEM,(Integer) map.get(Data.GAMEPLAYITEM));
			res.put(Data.GRAPHISMITEM, (Integer)map.get(Data.GRAPHISMITEM));
			res.put(Data.SOUNDITEM, (Integer)map.get(Data.SOUNDITEM));
			res.put(Data.CREATIVITYITEM, (Integer)map.get(Data.CREATIVITYITEM));
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}

	private String serialize() {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(Data.STAFFNAME,((XCommandAgent)myAgent).getStaffName());
		StringWriter sw = new StringWriter();
		ObjectMapper mapper = new ObjectMapper();
		try {
			mapper.writeValue(sw,map);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sw.toString();
	}

}
